package com.wlk.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wlk.mapper.GrantRecordMapper;
import com.wlk.pojo.GrantRecord;
import com.wlk.pojo.dto.AddGrantRecordDTO;
import com.wlk.service.GrantRecordServcie;
import com.wlk.utils.Result;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class GrantRecordServcieImpl extends ServiceImpl<GrantRecordMapper, GrantRecord> implements GrantRecordServcie {
    @Resource
    private GrantRecordMapper grantRecordMapper;

    @Override
    public Result addGrantRecord(AddGrantRecordDTO addGrantRecordDTO) {
        //验证数据是否为空
        Integer grantedId = addGrantRecordDTO.getGrantedId();
        String proCode = addGrantRecordDTO.getProCode();
        String depCode = addGrantRecordDTO.getDepCode();
        //对每个数据都进行非空校验
        if(grantedId==null||proCode.isEmpty()||depCode.isEmpty()){
            return Result.fail("数据不能为空");
        }
        // 查看是否已经被授权过
        Integer isGranted  = grantRecordMapper.getCountByProCodeAndGrantedId(proCode, grantedId);
        if (isGranted > 0){
            return Result.fail("该用户已经授权过该项目");
        }
        if (grantRecordMapper.addGrantRecord(addGrantRecordDTO) > 0){
            return Result.ok("授权成功");
        }else {
            return Result.fail("授权失败");
        }
    }
}
